<template>
  <div class="dashboard-container">
    <div class="app-container" />
    <el-card class="box-card">
      <el-tabs v-model="activeName">
        <el-tab-pane label="用户管理" name="first">
          <el-row style="height: 60px">
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="addList"
            >新增角色</el-button>
          </el-row>
          <el-table border="" :data="roleList">
            <!-- <el-table-column type="index" label="序号" /> -->
            <el-table-column label="序号">
              <template slot-scope="{$index }">
                {{ (page.page-1)*page.pagesize+$index+1 }}
              </template>
            </el-table-column>
            <el-table-column prop="name" label="角色名称" width="240" />
            <el-table-column prop="description" label="描述" />
            <el-table-column label="操作">
              <template slot-scope="{row}">
                <el-button size="small" type="success" @click="allotPermission(row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="editList(row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="delList(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row
            type="flex"
            justify="center"
            align="middle"
            style="height: 60px"
          >
            <!-- 分页组件 -->
            <el-pagination
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              :page-size="page.pagesize"
              :page-sizes="[2, 4, 6, 8]"
              @current-change="changeCurrent"
              @size-change="SizeChange"
            /></el-row>
          <!-- 公司信息 -->
        </el-tab-pane>
        <el-tab-pane label="配置管理" name="second">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top: 50px">
            <el-form-item label="公司名称">
              <el-input
                v-model="getcompanyData.name"
                disabled
                style="width: 400px"
              />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input
                v-model="getcompanyData.companyAddress"
                disabled
                style="width: 400px"
              />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input
                v-model="getcompanyData.mailbox"
                disabled
                style="width: 400px"
              />
            </el-form-item>
            <el-form-item label="备注">
              <el-input
                v-model="getcompanyData.remarks"
                type="textarea"
                :rows="3"
                disabled
                style="width: 400px"
              />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <el-dialog :title="title" :visible="isShow" :destroy-on-close="!isShow" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 点击分配权限 -->
      <el-dialog
        title="分配权限"
        :visible="showPermission"
        @close="treeCancel"
      >
        <el-tree
          ref="perissionTree"
          :data="permissionList"
          :props="{label:'name'}"
          default-expand-all
          show-checkbox
          node-key="id"
          :default-checked-keys="checkedKeys"
          :check-strictly="true"
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="treeCancel">取消</el-button>
            <el-button size="small" type="primary" @click="treeOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
import { addrole, delroleById, editroleById, getCompanyById, getroleList, permissionListById, roleListById } from '@/api/setting'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      // 点击分配权限
      showPermission: false,
      permissionList: [],
      checkedKeys: [],
      userId: null,
      activeName: 'first',
      getcompanyData: {},
      total: null,
      roleList: [],
      page: {
        page: 1,
        pagesize: 2
      },
      isShow: false,
      roleForm: {
        name: '',
        description: ''
      },
      // 表单验证
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' },
          { min: 1, max: 200, message: '长度在 1 到 200个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['getcompanyId']),
    title() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getcompany()
    this.getroleList()
  },
  methods: {
    // page,pagesize获取角色列表
    async getroleList() {
      const { rows, total } = await getroleList(this.page)
      this.roleList = rows
      this.total = total
    },
    // 根据id获取公司数据
    async getcompany() {
      this.getcompanyData = await getCompanyById(this.getcompanyId)
    },
    changeCurrent(val) {
      this.page.page = val
      this.getroleList()
    },
    SizeChange(val) {
      this.page.pagesize = val
      this.getroleList()
    },
    // 删除list
    async delList(id) {
      try {
        await this.$confirm('是否确认删除该部门', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delroleById(id)
        await this.getroleList()
        this.$message.success('操作成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 编辑 list
    async editList(id) {
      this.roleForm = await roleListById(id)
      this.isShow = true
    },
    // 新增list
    async addList() {
      this.isShow = true
      //
    },
    // 点击确定
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          await editroleById(this.roleForm)
        } else {
          await addrole(this.roleForm)
        }

        await this.getroleList()
        this.isShow = false
        this.$message.success('操作成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 取消按钮
    async btnCancel() {
      this.isShow = false
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 点击分配权限
    async allotPermission(id) {
      this.userId = id
      const res = await getPermissionList()
      this.permissionList = tranListToTreeData(res, '0')
      const { permIds } = await roleListById(id)
      this.checkedKeys = permIds
      this.showPermission = true
    },
    // 点击权限确定
    async treeOK() {
      await permissionListById({ id: this.userId, permIds: this.$refs.perissionTree.getCheckedKeys() })
      this.$message.success('更改成功')
      this.showPermission = false
    },
    // 点击取消权限
    treeCancel() {
      this.showPermission = false
      this.checkedKeys = []
    }
  }
}
</script>

<style>
</style>
